package com.projectName.www.servlet;

import com.projectName.www.util.MD5Util;
import com.projectName.www.dao.UserDaoImpl;
import com.projectName.www.po.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
⽀持游客登录, 游客登录的⽤⼾拥有拥有以下权限:
a. 可以浏览商家店铺, 可以进⼊店铺查看店铺提供的菜品
b. 可以使⽤搜索功能（模糊查询，敏感词过滤），可以浏览商品评论
 */
@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
    public LoginServlet() {
        super();
    }


    private UserDaoImpl userDaoImpl = null;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String userType = request.getParameter("userType");//选择登录的身份
//        if(userType.equals("")){
//
//        }
        //调用login方法
        User user = login(request);//加密
        if (user != null) {
            String userType1 = Integer.toString(user.getUserType());//用户的身份id
            System.out.println("loginservlet  -----------------------------------------");
            System.out.println(user);
            Integer onlinestatus = user.getOnlinestatus();
            if(onlinestatus==0){
                response.getWriter().write("your account is banned ,ask administrator for details!!!");

            }
            else if(userType1.equals(userType)){
                //设置参数
                request.getSession().setAttribute("user", user);
                //转发页面
                if(userType1.equals("1")){
                    request.getRequestDispatcher("/adminlogin.jsp").forward(request, response);
                }else if(userType1.equals("2")){
                    request.getRequestDispatcher("/salelogin.jsp").forward(request, response);
                }else if(userType1.equals("3")){
                    request.getRequestDispatcher("/ctmlogin.jsp").forward(request, response);
                }else if(userType1.equals("4")){
                    request.getRequestDispatcher("/tourists.jsp").forward(request, response);
                }

            }else {
                request.setAttribute("error","用户类型错误！");
                request.getRequestDispatcher("login.jsp").forward(request,response);
            }


        } else {
            request.setAttribute("error","账号或密码错误!!!!!!");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    public User login(HttpServletRequest request) {

        //请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        userDaoImpl = new UserDaoImpl();
        User user = userDaoImpl.queryByUsername(username);

        if (user != null){
            if (MD5Util.string2MD5(password).equals(user.getPassword())) {
                return user;
            } else {
                return null;
            }
        }
        return null;
    }
}
